package com.example.api.service.impl;

import com.example.api.mapper.VehicleMapper;
import com.example.api.model.entity.Vehicle;
import com.example.api.service.VehicleService;
import com.example.api.utils.DataTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class VehicleServiceImpl implements VehicleService {

    @Resource
    private VehicleMapper vehicleMapper;

    @Override
    public Vehicle save(Vehicle vehicle) {
        // 如果是新车辆，设置ID
        if (vehicle.getId() == null || vehicle.getId().isEmpty()) {
            vehicle.setId(UUID.randomUUID().toString());
        }
        vehicle.setCreateAt(DataTimeUtil.getNowTimeString());
        vehicleMapper.insert(vehicle);
        return vehicle;
    }

    @Override
    public void update(Vehicle vehicle) {
        vehicleMapper.update(vehicle);
    }

    @Override
    public void delete(String id) {
        vehicleMapper.deleteById(id);
    }

    @Override
    public Vehicle findById(String id) {
        return vehicleMapper.findById(id);
    }

    @Override
    public List<Vehicle> findAll() {
        return vehicleMapper.findAll();
    }
}
